package com.comdo.newup_loan.module.notice;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.model.NoticeDetailBean;
import com.comdo.newup_loan.util.TitleMenuUtil;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/11/3.
 */

public class NoticeDetailActivity extends BaseActivity<NoticeDetailPersenter> implements NoticeDetailContact.View {
    @BindView(R.id.webView)
    WebView mWebView;
    @BindView(R.id.tv_title)
    TextView mTitleText;
    @BindView(R.id.tv_date)
    TextView mDateText;
    private ProgressDialog dialog = null;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_notice_detail;
    }

    @Override
    protected void init() {
        super.init();
        mPresenter.getNoticeDetail(getIntent().getIntExtra("id",0));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(NoticeDetailActivity.this,"公告详情",true);
        WebSettings wSet = mWebView.getSettings();
        wSet.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (dialog == null || !dialog.isShowing()) {
                    dialog = new ProgressDialog(NoticeDetailActivity.this);
                    dialog.setMessage("页面加载中，请稍后..");
                }
                dialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }

    @Override
    public NoticeDetailPersenter getmPresenter() {
        return new NoticeDetailPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getNoticeDetail(NoticeDetailBean noticeDetailBean) {
        if(noticeDetailBean!=null){
            mTitleText.setText(noticeDetailBean.getTitle());
            mDateText.setText(noticeDetailBean.getCreateTime());
            mWebView.loadData(noticeDetailBean.getContent(), "text/html; charset=UTF-8", null);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }
}
